package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryItemServiceImpl implements ISystemDictionaryItemService {

    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;

    @Override
    public void delete(Long id) {
        systemDictionaryItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(SystemDictionaryItem systemDictionaryItem) {
        //如果用户没有填写序号的话  就给设置一个最大值放进数据库
        if (systemDictionaryItem.getSequence() == null) {
            int max = systemDictionaryItemMapper.selectMaxSequenceByParentId(systemDictionaryItem.getParentId());
            systemDictionaryItem.setSequence(max + 1);
        }
        systemDictionaryItemMapper.insert(systemDictionaryItem);
    }

    @Override
    public SystemDictionaryItem get(Long id) {
        return systemDictionaryItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionaryItem> listAll() {
        return systemDictionaryItemMapper.selectAll();
    }

    @Override
    public void update(SystemDictionaryItem systemDictionaryItem) {
        systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
    }

    public PageInfo list(QueryObject queryObject) {
        int currentPage = queryObject.getCurrentPage();
        int pageSize = queryObject.getPageSize();
        PageHelper.startPage(currentPage, pageSize, "sequence");
        List<SystemDictionaryItem> list = systemDictionaryItemMapper.list(queryObject);
        System.out.println(list);
        return new PageInfo(list);
    }

    @Override
    public int selectMaxSequenceByParentId(Long id) {
        int max = systemDictionaryItemMapper.selectMaxSequenceByParentId(id);
        return max;
    }

    @Override
    public List<SystemDictionaryItem> selectCourse() {
        return systemDictionaryItemMapper.selectCourse();
    }
}
